package com.framework.shardingsphere.utils;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class ShardingUtils {

    /**
     * 两个日期之间所有月份
     */
    public static List<String> getSuffixListForRangeByMonth(String lowerDate, String upperDate) {
        DateTime st = DateUtil.parseDateTime(lowerDate);
        DateTime et = DateUtil.parseDateTime(upperDate);
        DateTime ct = DateUtil.parseDateTime(DateUtil.format(new Date(), "yyyy-MM-dd 23:59:59"));
        //判断结束时间不能大于当前时间
        if (et.compareTo(ct) > 0) {
            et = ct;
        }
        List<DateTime> dateTimes = DateUtil.rangeToList(st, et, DateField.DAY_OF_MONTH);
        List<String> collect = dateTimes.stream().map(x -> x.toString("yyyyMM")).collect(Collectors.toList());
        HashSet h = new HashSet(collect);
        List<String> suffixList = ListUtil.toList(h);
        return suffixList;
    }
}
